﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Colorful.Control.Controls
{
    public class FXWindow : Window
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (IsOverridingWindowsChrome)
            {
                GetTemplatedChild<FrameworkElement>("PART_Caption").MouseLeftButtonDown += DoDragMove;
                GetTemplatedChild<Button>("PART_Close").Click += Close;
            }
        }

        private TElement GetTemplatedChild<TElement>(string elementName) where TElement : DependencyObject
        {
            var o = GetTemplateChild(elementName);
            if (o == null)
                throw new ArgumentException("element name not found or the provided type is invalid.", elementName);

            return (TElement)o;
        }

        protected override void OnInitialized(EventArgs e)
        {
            if (IsOverridingWindowsChrome)
                SetResourceReference(StyleProperty, "WindowsChromeOverride");

            base.OnInitialized(e);
        }

        /// <summary>
        /// Determines if window should override the chorme.
        /// </summary>
        public virtual bool IsOverridingWindowsChrome
        {
            get { return true; }
        }

        private void Close(object sender, EventArgs e)
        {
            Close();
        }

        private void DoDragMove(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    }
}
